<html>
<head><meta charset="utf-8"><title>who initialized LOCAL_STDOUT in stdio.rs · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/who.20initialized.20LOCAL_STDOUT.20in.20stdio.2Ers.html">who initialized LOCAL_STDOUT in stdio.rs</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="196340396"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/who%20initialized%20LOCAL_STDOUT%20in%20stdio.rs/near/196340396" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> yunhua <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/who.20initialized.20LOCAL_STDOUT.20in.20stdio.2Ers.html#196340396">(May 05 2020 at 17:35)</a>:</h4>
<p>Hi I'm trying to understand how stdio::_print_to works internally,  in libstd/io/stdio.rs</p>
<div class="codehilite"><pre><span></span><code><span class="n">thread_local</span><span class="o">!</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="sd">/// Stdout used by print! and println! macros</span>
<span class="w">    </span><span class="k">static</span><span class="w"> </span><span class="n">LOCAL_STDOUT</span>: <span class="nc">RefCell</span><span class="o">&lt;</span><span class="nb">Option</span><span class="o">&lt;</span><span class="nb">Box</span><span class="o">&lt;</span><span class="k">dyn</span><span class="w"> </span><span class="n">Write</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nb">Send</span><span class="o">&gt;&gt;&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">RefCell</span>::<span class="n">new</span><span class="p">(</span><span class="nb">None</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>LOCAL_STDOUT was initialized to None, but when I run a simple  "Hello Rust" program with rust-gdb, I saw the value is not None.<br>
 p local_s<br>
$1 = (struct LocalKey&lt;core::cell::RefCell&lt;core::option::Option&lt;alloc::boxed::Box&lt;Write&gt;&gt;&gt;&gt; *) 0x5555558227d8<br>
My question is who initialized LOCAL_STDOUT? Thanks.</p>



<a name="196340545"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/who%20initialized%20LOCAL_STDOUT%20in%20stdio.rs/near/196340545" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/who.20initialized.20LOCAL_STDOUT.20in.20stdio.2Ers.html#196340545">(May 05 2020 at 17:37)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/blob/f8d394e5184fe3af761ea1e5ba73f993cfb36dfe/src/libstd/io/stdio.rs#L524-L542" title="https://github.com/rust-lang/rust/blob/f8d394e5184fe3af761ea1e5ba73f993cfb36dfe/src/libstd/io/stdio.rs#L524-L542">https://github.com/rust-lang/rust/blob/f8d394e5184fe3af761ea1e5ba73f993cfb36dfe/src/libstd/io/stdio.rs#L524-L542</a></p>



<a name="196340653"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/who%20initialized%20LOCAL_STDOUT%20in%20stdio.rs/near/196340653" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/who.20initialized.20LOCAL_STDOUT.20in.20stdio.2Ers.html#196340653">(May 05 2020 at 17:38)</a>:</h4>
<p>Wait, wrong code</p>



<a name="196341255"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/who%20initialized%20LOCAL_STDOUT%20in%20stdio.rs/near/196341255" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/who.20initialized.20LOCAL_STDOUT.20in.20stdio.2Ers.html#196341255">(May 05 2020 at 17:43)</a>:</h4>
<p><code>LOCAL_STDOUT</code> should only be <code>Some</code> when during tests.</p>
<p>Looking at the code I see that <code>_print calls </code>print_to<code> with a reference to </code>LOCAL_STDOUT`: <a href="https://github.com/rust-lang/rust/blob/f8d394e5184fe3af761ea1e5ba73f993cfb36dfe/src/libstd/io/stdio.rs#L890" title="https://github.com/rust-lang/rust/blob/f8d394e5184fe3af761ea1e5ba73f993cfb36dfe/src/libstd/io/stdio.rs#L890">https://github.com/rust-lang/rust/blob/f8d394e5184fe3af761ea1e5ba73f993cfb36dfe/src/libstd/io/stdio.rs#L890</a></p>
<p><code>print_to</code> then simply uses the global output when <code>LOCAL_STDOUT</code> is none: <a href="https://github.com/rust-lang/rust/blob/f8d394e5184fe3af761ea1e5ba73f993cfb36dfe/src/libstd/io/stdio.rs#L854-L880" title="https://github.com/rust-lang/rust/blob/f8d394e5184fe3af761ea1e5ba73f993cfb36dfe/src/libstd/io/stdio.rs#L854-L880">https://github.com/rust-lang/rust/blob/f8d394e5184fe3af761ea1e5ba73f993cfb36dfe/src/libstd/io/stdio.rs#L854-L880</a></p>



<a name="196342869"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/who%20initialized%20LOCAL_STDOUT%20in%20stdio.rs/near/196342869" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> yunhua <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/who.20initialized.20LOCAL_STDOUT.20in.20stdio.2Ers.html#196342869">(May 05 2020 at 17:57)</a>:</h4>
<p>Thanks! <span class="user-mention" data-user-id="133247">@bjorn3</span>    seems LOCAL_STDOUT is not none in my simple "Hello Rust" program.  I compiled rustc by myself, so line number is little different. </p>
<div class="codehilite"><pre><span></span><code>Breakpoint 1, std::io::stdio::print_to::_$u7b$$u7b$closure$u7d$$u7d$::h42c135b27158b526 (s=0x7ffff7fa0760) at src/libstd/io/stdio.rs:781
781             if let Ok(mut borrowed) = s.try_borrow_mut() {
(gdb) p s
$2 = (struct RefCell&lt;core::option::Option&lt;alloc::boxed::Box&lt;Write&gt;&gt;&gt; *) 0x7ffff7fa0760
(gdb) n
781             if let Ok(mut borrowed) = s.try_borrow_mut() {
782                 if let Some(w) = borrowed.as_mut() {
(gdb) p borrowed
$3 = RefMut&lt;core::option::Option&lt;alloc::boxed::Box&lt;Write&gt;&gt;&gt; = {value = 0x7ffff7fa0768, borrow = BorrowRefMut = {borrow = 0x7ffff7fa0760}}
</code></pre></div>


<p>borrowed.as_mut() returned None, so it called global_s().write_fmt(args)<br>
I don't understand who set LOCAL_STDOUT to Some()</p>
<p>using grep, I found  LOCAL_STDOUT in src/librustc_codegen_llvm/consts.rs, not sure if that related.</p>



<a name="196350178"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/who%20initialized%20LOCAL_STDOUT%20in%20stdio.rs/near/196350178" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/who.20initialized.20LOCAL_STDOUT.20in.20stdio.2Ers.html#196350178">(May 05 2020 at 18:57)</a>:</h4>
<p>What does <code>borrowed.value</code> dereference to? <code>borrowed.value</code> is a pointer to the <code>Option&lt;Box&lt;dyn Write&gt;&gt;</code>.</p>



<a name="196358672"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/who%20initialized%20LOCAL_STDOUT%20in%20stdio.rs/near/196358672" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/who.20initialized.20LOCAL_STDOUT.20in.20stdio.2Ers.html#196358672">(May 05 2020 at 19:55)</a>:</h4>
<p><span class="user-mention" data-user-id="252928">@yunhua</span> ^</p>



<a name="196362048"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/who%20initialized%20LOCAL_STDOUT%20in%20stdio.rs/near/196362048" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> yunhua <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/who.20initialized.20LOCAL_STDOUT.20in.20stdio.2Ers.html#196362048">(May 05 2020 at 20:23)</a>:</h4>
<p><span class="user-mention" data-user-id="133247">@bjorn3</span> </p>
<div class="codehilite"><pre><span></span><code>(gdb) p borrowed
$16 = RefMut&lt;core::option::Option&lt;alloc::boxed::Box&lt;Write&gt;&gt;&gt; = {value = 0x7ffff7fa0768, borrow = BorrowRefMut = {borrow = 0x7ffff7fa0760}}
(gdb) p *borrowed.value
$17 = Option&lt;alloc::boxed::Box&lt;Write&gt;&gt;
(gdb) x/2w borrowed.value
0x7ffff7fa0768: 0       0
</code></pre></div>


<p>So can we confirm borrowed.value is None here?  As x/2w given 0,0.   Thanks.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>